pspsdkfandomcom-20200213-history
Homebrew Programming in PYTHON
Thanks to Acid_Snake at the wololo.net forums for this great tutorial and for giving me permission to put it here. The Tutorial Python Programming on the psp is very similar to pc programming, the only new thing here is the module psp2d. First you need stackless python psp: http://code.google.com/p/pspstacklesspy ... p&can=2&q= Your main application will be stored in a file called script.py, placed in the same place as eboot.pbp (the interpreter). This module provides a 2d graphics library for all your python needs. Make sure psp2d.py is in the same place as script.py. You can find it here:http://code.google.com/p/pspstacklesspy ... p&can=2&q= Font: Psp2d uses sfonts which you can find by google searching. Here is an example: http://code.google.com/p/pspstacklesspy ... p&can=2&q= Let's start with the basics, and what better way to learn than with our typical hello world. import psp2d font = psp2d.Font("font.png") image = psp2d.Image(480, 272) screen = psp2d.Screen() CLEAR_COLOR = psp2d.Color(0,0,0) image.clear(CLEAR_COLOR) font.drawText(image, 0, 0, "Hello World") screen.blit(image) screen.swap() Let's go line by line: font = psp2d.Font("font.png") Used to open the font file (font.png) image = psp2d.Image(480, 272) This creates the image that will be drawn on the screen, it can also be a background: image = psp2d.Image("background.png") The psp screen, not much to say about this. CLEAR_COLOR = psp2d.Color(0,0,0) The color that will be painted on screen, it works on a RGB basis. image.clear(CLEAR_COLOR) Pretty self-explaining, you clear image with the clear color. font.drawText(image, 0, 0, "Hello World") This is the meat of the code, it draws the text on screen. It works on a coordinate basis, the first 0 is the x coordinate (left-right), the second 0 is the y coordinate (up-down). Play around with it a bit. screen.blit(image) Basically, screen becomes what image is. screen.swap() Update screen. Controller: Psp2d has access to all the buttons on the psp except the note key, the screen key, the volume keys, and the home key. It is pretty simple to use: pad = psp2d.Controller() and: pad.up pad.down pad.left pad.right pad.cross pad.circle pad.square pad.triangle pad.l pad.r pad.start pad.select pad.analogX pad.analogY Now, you don't really call pad.x, you use it with the if statement: if pad.cross: do something elif pad.circle: do something different psp2d.Controller requires a loop with the while statement: while True: pad = psp2d.Controller() if pad.cross: do something Now this is not the way to do this, while True is an endless loop because True is always True, so there is no way to end it, we don't want that, we want to be able to end the loop and to do so we use a variable since it's modifiable: x = True while x True: pad = psp2d.Controller() if pad.circle: x = False by pressing the circle button we modify x to become False, by becoming false the condition is no longer met and therefore the loop breaks. Let's put all the above into a live example: import psp2d font = psp2d.Font("font.png") image = psp2d.Image(480, 272) screen = psp2d.Screen() CLEAR_COLOR = psp2d.Color(0,0,0) image.clear(CLEAR_COLOR) font.drawText(image, 0, 0, "Hello World!") font.drawText(image, 0, 30, "Press Circle to exit") screen.blit(image) screen.swap() x = True while x True: pad = psp2d.Controller() if pad.circle: font.drawText(image, 0, 60, "Goodbye!") screen.blit(image) screen.swap() x = False The result ; Running psp python homebrews on pc Since python is cross-platform and the psp2d.py file acts as a muck-up of pygame you can play and debug all psp homebrews written in python on a pc. To do this you need to install pygame (google it). Here is a screenshot of various psp python homebrews running on a pc: ; Category:Basics Category:Tutorials